// Quantitative Angiogram Analysis System
//
// Copyright ?Ping Zou, 2006
// sg71.cherub@gmail.com

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.IO;

using WeifenLuo.WinFormsUI;
using eee.Sheffield.PZ.Imaging;
using eee.Sheffield.PZ.Imaging.ImageIO;
using eee.Sheffield.PZ.Imaging.ImageFilter;

namespace eee.Sheffield.PZ.QAAS
{
    /// <summary>
    /// image doc class
    /// </summary>
    public partial class ImageDoc : DockContent
    {
        #region Fields
        //private bool _modified = false;
        private Bitmap _image = null;
        private string _fileName = null;
        private int width = 0;
        private int height = 0;
        private string _log;
        #endregion

        #region Properties
        public Bitmap Image { get { return _image; } }
        public int ImageWidth { get { return width; } }
        public int ImageHeight { get { return height; } }
        public string ImageFileName { get { return _fileName; } set { _fileName = value; } }
        public string LogText { get { return _log; } }
        #endregion

        #region Constructors
        // From Image
        public ImageDoc(Bitmap image)
        {
            this._image = image;
            // standarize image
            //eee.Sheffield.PZ.Imaging.Image.StandardizeImageTo8bbpIndexed(this.image);
            Init();
        }

        // From File
        public ImageDoc(String fileName)
        {
            // load image
            IImageIO imageIO = null;

            // check file name extension to choose corresponding ImageIO class
            string extension = Path.GetExtension(fileName);
            extension = extension.ToLower();
            bool isSupported = true;
            switch (extension)
            {
                case ".pgm":
                    imageIO = new PGMImageIO(fileName);                    
                    break;
                case ".ppm":
                    imageIO = new PPMImageIO(fileName);                    
                    break;
                case ".dcm":
                    imageIO = new DICOMImageIO(fileName);
                    break;
                case ".bmp":
                    imageIO = new BMPImageIO(fileName);                    
                    break;
                case ".jpg":
                    imageIO = new JPGImageIO(fileName);
                    break;
                case ".png":
                    imageIO = new PNGImageIO(fileName);
                    break;
                case ".tif":
                    imageIO = new TIFImageIO(fileName);
                    break;
                default:
                    isSupported = false;                    
                    break;
            }

            if (!isSupported)
                throw new ApplicationException("ImageIO::LoadImage(), Unkown image type");
            else
            {
                imageIO.LoadImage();
                _image = (Bitmap)imageIO.Image.Clone();
                this._fileName = fileName;
                Init();
            }
        }

        #region Initialization Processes
        private void Init()
        {
            InitializeComponent();
            // set form style
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw, true);
            //this.AutoScroll = true;
            //Invalidate();
        }
        #endregion
        #endregion

        #region Apply filters        
        /// <summary>
        /// apply image filter
        /// </summary>
        /// <param name="filter">ImageFilter base class</param>
        private void ApplyImageFilter(ImageFilter filter)
        {
            try
            {
                // set wait cursor
                this.Cursor = Cursors.WaitCursor;

                Bitmap newImage = filter.Apply(_image);

                ShowImage(newImage, this.TabText + "*");

                //_image.Dispose();
                //_image = newImage;

                //Invalidate();

                // record modified
                //if (!_modified)
                //    this.TabText = this.TabText + "*";
                //_modified = true;
                _log = "Image <" + this.TabText + "> : " + filter.Flag + " " + filter.LogText;
            }
            catch (ArgumentException)
            {
                string errorText = "ImageDoc::ApplyFilter(), Selected filter can not be applied to the image";
                MessageBox.Show(errorText, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                _log = errorText;
            }
            finally
            {
                // restore cursor
                this.Cursor = Cursors.Default;
            }
        }
        #endregion

        #region uitl methods
        private void Log()
        {
            ((MainForm)this.MdiParent).Log(_log);
        } // Log()

        private void Log(string logText)
        {
            ((MainForm)this.MdiParent).Log(logText);
        } // Log()

        public void ShowImage(Bitmap image, string caption)
        {
            ((MainForm)this.MdiParent).ShowImage(image, caption);
        } // ShowImage()

        private void UseFilterForm(FilterFormType filterFormType)
        {
            ((MainForm)this.MdiParent).ShowFilterForm();
            IFilterControl filterControl = null;
            MainForm mainForm = (MainForm)this.MdiParent;
            switch (filterFormType)
            {
                case FilterFormType.FloodFill:
                    filterControl = new FloodFillFilterControl(mainForm, this);
                    break;
                case FilterFormType.GVF:
                    filterControl = new GVFFilterControl(mainForm, this);
                    break;
                case FilterFormType.Threshold:
                    filterControl = new ThresholdFilterControl(mainForm, this);
                    break;
                case FilterFormType.MedianSmooth:
                    filterControl = new MedianFilterControl(mainForm, this);
                    break;
                case FilterFormType.TwoImages:
                    filterControl = new TwoImagesFilterControl(mainForm, this);
                    break;
                case FilterFormType.GVFThinning:
                    filterControl = new GVFThinningFilterControl(mainForm, this);
                    break;
                case FilterFormType.ZouRockettTracking:
                    filterControl = new ZouRockettTrackingFilterControl(mainForm, this);
                    break;
                default:
                    break;
            }
            ((MainForm)this.MdiParent).filterForm.Set(filterControl);

            //((MainForm)this.MdiParent).filterForm.ImageDoc = this;
            //((MainForm)this.MdiParent).filterForm.Set(filterFormType);
            //((MainForm)this.MdiParent).filterForm.BringToFront();
        } // UseFilterForm()
        #endregion

        #region Event
        // Paint image
        protected override void OnPaint(PaintEventArgs e)
        {
            if (_image != null)
            {
                int width = _image.Width;
                int height = _image.Height;
                int x = 0;
                //int y = 0;
                int y = menuStripImageDoc.Height;
                Graphics g = e.Graphics;
                // draw image
                g.DrawImage(_image, x, y, width, height);
            }
        }
        #endregion






































































    }
}